import json

print('综能电量采集...')

class TZn645:
    def __init__(self):
        self.config = {
            "comPort":"COM1",
        }
    def getCmdBytes (self , addr , diList ):
        res = None
        try:
            cmdBytes = []
            addrBytes = bytes(reversed(bytes.fromhex(addr)))
            cmdBytes.append(0x68)
            for i in range(len(addrBytes)):
                cmdBytes.append(addrBytes[i])
            cmdBytes.append(0x68)
            cmdBytes.append(0x11)
            cmdBytes.append(0x4)

            cmdBytes.append(0x33 + diList[3])
            cmdBytes.append(0x33 + diList[2])
            cmdBytes.append(0x33 + diList[1])
            cmdBytes.append(0x33 + diList[0])
            cs = 0
            for i in range(len(cmdBytes)):
                cs = cs + cmdBytes[i]
                cs = cs % 256
            cmdBytes.append(cs)
            cmdBytes.append(0x16)
            res = bytearray(cmdBytes)
            cmdBytes = None
        except Exception as er:
            print(er)
        return  res



fn = "C:/Users/Administrator/Documents/WeChat Files/wxid_vzvp2wizqvm512/FileStorage/File/2024-04/access240427_001/access.log"

with open(fn) as f:
    contexts = f.readlines()

# define ip dict###
ip = {}  # key为ip信息，value为ip数量（若重复则只增加数量）
flow = {}  # key为ip信息，value为流量总和
sum = 0.0

datas={}
logItems=[]


for line in contexts:
    item = line.split()
    logItems.append({
        "ip":item[0] ,
        "method":item[5],
        "inf":item[6]
    })
csvStr ='\n'.join( [x['ip'] +'\t'+x['inf'] for x in logItems])
with open('logitems.txt','w') as f :
    f.write(csvStr)
    f.flush()

print(csvStr)

for line in contexts:
    item = line.split()
    logItems.append({
        "ip":item[0] ,
        "method":item[5],
        "inf":item[6]
    })
    ip = item[0]
    if ip in datas.keys():
        datas[ip]['count'] = datas[ip]['count'] - 1
    else:
        dtStr = item[3]
        method = item[4]
        apiPath = item[5]
        statusCode = item[7]
        respSize = item[8]
        info = {
            "ip":ip ,
            "dtstr":dtStr ,
            "method":method ,
            "api":apiPath ,
            "status":statusCode ,
            "size":respSize ,
            "count":0
        }
        datas[ip] = info
    tm = item[3]+' '+item[4]
    tm = tm.replace('[','')
    tm = tm.replace(']','')
    datas[ip]['tm'] = tm

logStr = json.dumps(logItems)
csvStr ='\n'.join( [x['ip'] +'\t'+x['inf'] for x in logItems])
with open('logitems.json','w') as f :
    f.write(logStr)
    f.flush()
with open('logitems.txt','w') as f :
    f.write(csvStr)
    f.flush()
dataList = list(datas.values())
sortData = sorted(dataList , key=lambda item: item['count'])
filterData = sortData[:1000]
s= "\n".join([ x['ip']+'\t'+str(x['count'])+'\t'+ x['tm'] for x in filterData])
print(s)
dataStr = json.dumps(filterData)
print(dataStr)
#print(sortData)